package com.example.calender.ui;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.example.calender.BaseActivity;
import com.example.calender.BaseApplication;
import com.example.calender.R;
import com.example.calender.persistence.User;
import com.example.calender.utils.DialogOneEditUtils;
import com.example.calender.utils.MD5Utils;
import com.example.calender.utils.SharedPreferencesUtils;
import com.example.calender.utils.ToastUtil;
import com.tbruyelle.rxpermissions3.RxPermissions;

public class LoginActivity extends BaseActivity {

    private static final String TAG = "LoginActivity";
    TextView textLogin ,textRegister,textForgetpassword;
    TextView loginTextView,passwordTextView;
    RxPermissions permissions;

    @Override
    protected int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void findViews() {
        textLogin = (TextView)findViewById(R.id.tv_login_login);
        textRegister=(TextView)findViewById(R.id.tv_login_register);
        textForgetpassword=(TextView)findViewById(R.id.tv_login_forgetpassword);
        loginTextView = (TextView)findViewById(R.id.editTextTextPersonName2);
        passwordTextView = (TextView)findViewById(R.id.editTextTextPassword2);
    }

    @Override
    protected void initView() {
        permissions = new RxPermissions(this);
        permissions.request(
                Manifest.permission.READ_CALENDAR,
                Manifest.permission.WRITE_CALENDAR)
                .subscribe(aBoolean -> {
                    if(!aBoolean){
                        DialogOneEditUtils.getInitialize(LoginActivity.this, new DialogOneEditUtils.DialogClick() {
                            @Override
                            public void cancelClick() {
                                finish();
                            }

                            @Override
                            public void determineClick() {
                                getAppDetailSettingIntent(LoginActivity.this);
                            }
                        },"请给予运行必要的权限");
                    }
                });
    }

    /**
     * 跳转到权限设置界面
     */
    private void getAppDetailSettingIntent(Context context){
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if(Build.VERSION.SDK_INT >= 9){
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if(Build.VERSION.SDK_INT <= 8){
            intent.setAction(Intent.ACTION_VIEW);
            intent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");
            intent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        startActivity(intent);
    }

    @Override
    protected void initListener() {
        textForgetpassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,ForgetPasswordActivity.class);
                startActivity(intent);
            }
        });
        textRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        textLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = loginTextView.getText().toString().trim();
                String password = passwordTextView.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    ToastUtil.showShort("账号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    ToastUtil.showShort("密码不能为空");
                    return;
                }
                threadPool.execute(() -> {
                    User user = ((BaseApplication) getApplication()).getUserDB().userDao().findByName(name);
                    if (user != null && MD5Utils.digest(password).equals(user.getPassword())) {
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        SharedPreferencesUtils.setParam("isLogin", true);
                        finish();
                    } else {
                        runOnUiThread(() -> ToastUtil.showShort("账号密码错误"));
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {

    }
}